/*
 * Exploder
 * 
 * Tero Pyylampi (218437), Jussi Tuurinkoski (211594)
 *
 * A medium sized bomb for game
 */

#include "GameBombMedium.h"
#include "../../../GameEngine.h"
#include "../../../GameCamera.h"
#include "../../../objects/physicobjects/GamePhysicObjectEngine.h"
#include "../../../misc/GameWorldModule.h"
#include "../../../layers/LayerGame.h"
#include "../../../effects/GameEffects.h"
#include "../../../../Resources.h"
#include "../../../../Common.h"

using namespace cocos2d;

const float EXPLOSION_FORCE = 0.06f;

// default constructor
GameBombMedium::GameBombMedium( b2World *world, cocos2d::CCLayer *layer, GameCamera *camera, b2Vec2 pos )
	: GameBomb( world, layer, camera ), mBombEffect( 0 ) {

	// init sprite
	CCSprite *tmpSprite;
	tmpSprite = CCSprite::spriteWithFile( getResource( Textures::GAME_BOMB_MEDIUM ) );
	tmpSprite->setScale( RATEX * 0.75f );
	layer->addChild( tmpSprite, Z_LAYER_GAME_FRONT );
	mSprites.push_back( tmpSprite );

	// create physics body for the bomb
	ObjectProperties properties( 0.01f, 0.55f, 0.2f );
	b2Body *mineBody = createCircle( false, 1.0f, pos.x, pos.y, properties );
	mBodies->push_back( mineBody );

	// create effect
	mBombEffect = GameEffects::createFireEffect();
	layer->addChild( mBombEffect, Z_LAYER_GAME_FRONT );
	mBombEffect->retain();
}

// default destructor
GameBombMedium::~GameBombMedium() {
	// release sprites
	for( unsigned int i = 0; i < mSprites.size(); ++i )
		mGameLayer->removeChild( mSprites.at( i ), true );
	mGameLayer->removeChild( mBombEffect, true );
	mBombEffect->release();
}

// update function
void GameBombMedium::update( cocos2d::ccTime dt ) {
	GameBomb::update( dt );
	
	// get body position & rotation
	b2Vec2 myWorldPos = mBodies->at( 0 )->GetPosition();
	cocos2d::CCPoint myScreenPos = mGameCamera->convertWorldToScreen( myWorldPos.x, myWorldPos.y );
	float myRotation = mBodies->at( 0 )->GetAngle() * -180.0f / 3.14159f;
	// set sprite position & rotation
	mSprites.at( 0 )->setPosition( myScreenPos );
	mSprites.at( 0 )->setRotation( myRotation );
	
	// set effect pos
	cocos2d::CCPoint pos = mSprites.at( 0 )->getPosition();
	mBombEffect->setPosition( ccp( pos.x + RATEX * 28.0f * std::cos( mBodies->at( 0 )->GetAngle() + 0.58f ), pos.y + RATEY * 28.0f * std::sin( mBodies->at( 0 )->GetAngle() + 0.58f ) ) );
}

// detonate bomb
void GameBombMedium::detonate() {
	// show effect
	b2Vec2 myWorldPos = mBodies->at( 0 )->GetPosition();
	cocos2d::CCPoint myScreenPos = mGameCamera->convertWorldToScreen( myWorldPos.x, myWorldPos.y );
	CCParticleSystem *s = GameEffects::createMediumExplosion( myScreenPos.x, myScreenPos.y );
	mGameLayer->addChild( s, Z_LAYER_GAME_EFFECTS );

	// cause physics explosion
	GameWorldModule::causePhysicWorldExplosion( mGameWorld, 
		dynamic_cast< LayerGame* >( mGameLayer )->getGameEngine()->getPhysicObjectEngine()->getPhysicObjects(), 
		mBodies->at( 0 )->GetPosition(), EXPLOSION_FORCE );

	// set this object to be removed
	mCanBeRemoved = true;
}

// get the main type of the object
PhysicObjectItems GameBombMedium::getObjectType() {
	return BOMB_SMALL_MINE;
}
